package com.qf.mapper;


import com.qf.model.Contractor;
import com.qf.model.Developer;
import com.qf.model.Project;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
@Mapper
public interface ProjectMapper {
    @Select("Select * from project where status =1")
    List<Project> findAll();
    //通过注解的方式实现映射
    //@Results 等价于 <ResultMap>标签
    //@Result  等价于<result>标签  column字段
    @Results(id = "projectMap",value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "pname",property = "pname"),
            @Result(column = "starttime",property ="starttime"),
            @Result(column = "endtime",property ="endtime"),
            @Result(column = "updatetime",property ="updatetime"),
            @Result(column = "did",property ="developer",javaType = Developer.class,one=
                    //FetchType.LAZY 懒加载   EAGER:立即加载
                    //select 代表的关联查询  调用指定的mapper方法
             @One(select = "com.qf.mapper.DeveloperMapper.findByDid" ,fetchType = FetchType.EAGER)
            ),
            @Result(column = "cid",property ="contractor",javaType = Contractor.class,one=
            @One(select = "com.qf.mapper.ContractorMapper.findByCid" ,fetchType = FetchType.EAGER)
            )
    })


    //分页查询  limit  0,10  currentPage当前页  pageSize每页大小
    @Select("Select * from project where status =1 limit #{currentPage},#{pageSize}")
    List<Project> findPage(@Param("currentPage")Integer currentPage, @Param("pageSize") Integer pageSize);

    // 查询总记录数
    @Select("select count(*) from project where status=1")
    Long count();

    @Insert("insert into project (pname,starttime,endtime,updatetime) values (#{pname},#{starttime},#{endtime},#{updatetime}) ")
    int addProject(Project project);

    @ResultMap("projectMap")//等同于复用resultMap
    @Select("Select * from project where pid =#{pid} and status = 1")
    Project selectByPid(int pid);
    //修改
    @Update("update project set pname=#{pname},starttime=#{starttime},endtime=#{endtime},updatetime=#{updatetime} " +
            "where pid=#{pid} and status=1")
    int edit(Project project);

    //logic delete 逻辑删除
    @Update("update project set status =0 where pid=#{pid}")
    int deleteByPid(Integer pid);
    @Select("select * from project where pid=#{pid} and status =1")
    Project findByPid(Integer pid);

    @Update("update project set did=#{did} where pid=#{pid}")
    void bindDid(@Param("did") String did,@Param("pid") int pid);

    @Update("update project set cid=#{cid} where pid=#{pid}")
    void bindCid(@Param("cid") String cid,@Param("pid") int pid);
    //代表查询开发商的工程集合

    @Select("select * from project where did=#{did} and status =1")
   List<Project> findByDid(String did);

    //代表查询承建商的工程集合
    @Select("select * from project where cid=#{cid} and status=1")
    List<Project> findByCid(String cid);
    //删除
    @Update("update project set status =0 where did=#{did} ")
    void deleteByDid(String did);

    @Update("update project set status =0 where cid=#{cid} ")
    void deleteByCid(String cid);
    //更新
    @Update("update project set did='' where did=#{did} and status =1")
    void deleteDid(String did);
    @Update("update project set cid='' where cid=#{cid} and status =1")
    void deleteCid(String cid);
}
